
var GAME = function(){
	var owner = this;
	owner.canvas = document.getElementById("canvas");
	owner.ctx = owner.canvas.getContext("2d");
	owner.ctx.lineWidth = 2;
	owner.width = 600;
	owner.height = 400;
	owner.x = 150;
	owner.y = 220;
	owner.left = 50;
	owner.top = 40;
	owner.plane = null;
	owner.stones = [];
	owner.breakPoints = [];
	owner.liveTimer = null;
	owner.deadTimer = null;
	
	owner.start = function(e){
		owner.canvas.width = owner.width;
		owner.canvas.height = owner.height;
		
		document.addEventListener("mousemove", owner.move ,false);
		owner.plane = new Player(e.clientX-owner.left, e.clientY-owner.top);
//		owner.plane.shield = 101;
		window.clearInterval(owner.deadTimer);
		owner.liveTimer = window.setInterval(owner.draw, 20);
	};
	
	owner.move = function(e){
		owner.plane.position.x = e.clientX-owner.left;
		owner.plane.position.y = e.clientY-owner.top;
	};
	
	owner.draw = function(){ 
		owner.ctx.clearRect(0, 0, owner.width, owner.height);
		//添加障碍物
		var stoneNum = owner.stones.length;
		var _stone = owner.addStone();
		stoneNum<50&&_stone!=null&&owner.stones.push(_stone); 
		for(var d=0;d<stoneNum;d++){
			var stone=owner.stones[d];
			if(stone==null) {
				continue;
			}
			owner.ctx.beginPath();
			owner.ctx.fillStyle="#ff0000";
			owner.ctx.arc(stone.position.x,stone.position.y,stone.size,0,Math.PI*2,true);
			owner.ctx.fill();
//			$("#console").prepend("<br />"+stone.distanceTo(owner.plane.position)+"_"+stone.size);
			if(stone.distanceTo(owner.plane.position)<stone.size+owner.plane.size) {
				owner.breakIt(owner.plane.position, 10);
				d=stoneNum;
				break;
			}
			stone.position.y += stone.force;
			if(stone.position.y > owner.height+stone.size){
				owner.stones.splice(d, 1);
			}
		}
		//添加保护罩
//		if(owner.plane.shield>0&&(owner.plane.shield>100||owner.plane.shield%3!=0)){
//			owner.ctx.beginPath();
//			owner.ctx.fillStyle="#167a66";
//			owner.ctx.strokeStyle="#00ffcc";
//			owner.ctx.arc(owner.plane.position.x,owner.plane.position.y,owner.plane.size*(Math.min(owner.plane.shield,100)/50),0,Math.PI*2,true);
//			owner.ctx.fill();
//			owner.ctx.stroke();
//		}
		owner.plane.trail.push(new Point(owner.plane.position.x,owner.plane.position.y));
		owner.ctx.beginPath();
		owner.ctx.strokeStyle="#ccc";
		owner.ctx.lineWidth = 3;
		var length = owner.plane.trail.length-1;
		for(var d=0; d<length; d++){
			p1=owner.plane.trail[d];
			p2=owner.plane.trail[d+1];
			owner.ctx.quadraticCurveTo(p1.position.x,p1.position.y,p1.position.x+(p2.position.x-p1.position.x)/2,p1.position.y+(p2.position.y-p1.position.y)/2);
			p1.position.y+=2;
		}
		owner.ctx.stroke();
		owner.ctx.closePath();
		owner.plane.trail.length>30&&owner.plane.trail.shift();
		owner.ctx.beginPath();
		owner.ctx.strokeStyle="#8ff1ff";
		owner.ctx.lineWidth = 2;
		owner.ctx.arc(owner.plane.position.x,owner.plane.position.y,owner.plane.size,Math.PI*5/6,Math.PI*1/6, false);
		owner.ctx.stroke();
		if(owner.plane.position.x<=0 || owner.plane.position.y<=0 || owner.plane.position.x>=owner.width || owner.plane.position.y>=owner.height) {
			owner.breakIt(owner.plane.position, 10);
		}
	};
	
	owner.breakIt = function(position, offset) {
		window.clearInterval(owner.liveTimer);
		for(var d=10+Math.random()*15; --d>=0;){
			var i=new Point(0, 0);
			i.position.x=position.x+Math.sin(d)*offset;
			i.position.y=position.y+Math.cos(d)*offset;
			i.velocity={x:-4+Math.random()*8,y:-4+Math.random()*8};
			i.alpha=1;
			owner.breakPoints.push(i);
		}
		owner.deadTimer = window.setInterval(function(){
			owner.ctx.clearRect(0, 0, owner.width, owner.height);
			for(var d=0;d<owner.breakPoints.length;d++){
				var p=owner.breakPoints[d];
				p.velocity.x+=(p.velocity.x)*0.04;
				p.velocity.y+=(p.velocity.y)*0.04;
				p.position.x+=p.velocity.x;
				p.position.y+=p.velocity.y;
				p.alpha-=0.02;
				owner.ctx.fillStyle="rgba(255,255,255,"+Math.max(p.alpha,0)+")";
				owner.ctx.fillRect(p.position.x,p.position.y,1,1);
				p.alpha<=0&&owner.breakPoints.splice(d,1);
			}
			if(owner.breakPoints.length==0) {
				window.clearInterval(owner.deadTimer);
			}
		}, 30);
	}
	
	owner.addStone = function(){
		var stone = new Enemy();
		if(Math.random()>0.2) {
			stone.position.x=Math.random()*owner.width;
			stone.position.y=-5;
			return stone;
		} else {
			return null;
		}
	};

	
	
};

var Point = function(_x, _y){
	this.position = {
		x: _x,
		y: _y
	}
}
Point.prototype.distanceTo = function(p) {
	var _x = p.x - this.position.x;
	var _y = p.y - this.position.y;
	return Math.sqrt(_x *_x + _y *_y);
};
Point.prototype.clonePosition = function() {
	return {
		x : this.position.x,
		y : this.position.y
	}
};
var Player = function(_x, _y){
	this.position = {
		x: _x,
		y: _y
	};
	this.trail = [];
	this.size = 8;
	this.shield = 0;
};
Player.prototype = new Point(0, 0);

var Enemy = function() {
	this.position = {
		x : 0,
		y : 0
	};
	this.size = 3 + Math.random() * 6;
	this.force = 3 + Math.random() * 3;
}
Enemy.prototype = new Point(0, 0);

var Shield = function() {
	this.position = {
		x : 0,
		y : 0
	};;
	this.size = 5 + Math.random() * 4;
	this.force = 1 + Math.random() * 0.5
}
Shield.prototype = new Point(0, 0);














